﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.BL.Helper;
using WebUI.BL.News;
using WebUI.DAL;
using WebUI.Models;

namespace WebUI.AdminSite.News
{
    public partial class NewsCreate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                LoadDll();
            }
        }

        private void LoadDll()
        {
            var repository = new NewsBL();
            var source = repository.GetDropDownListCategoryNews((Text,Value)=>new ItemDropDownList
                                                                                  {
                                                                                      Text = Text,
                                                                                      Value = Value.ToString()
                                                                                  }).ToList();

            drpCategory.DataSource = source;
            drpCategory.DataTextField = "Text";
            drpCategory.DataValueField = "Value";
            drpCategory.DataMember = "Text";
            drpCategory.DataBind();
        }

        protected void chkDisplay_CheckedChanged(object sender, EventArgs e)
        {
            int idImg = String.IsNullOrEmpty(hfdImgCode.Value.Trim()) ? 0 : int.Parse(hfdImgCode.Value.Trim());
            if(chkDisplay.Checked)
            {
                LoadImg(idImg);
            }
            else
            {
                imgDisplay.Visible = false;
            }  
        }

        private void LoadImg(int id)
        {
            imgDisplay.ImageUrl="../../DisplayImage.ashx?ImgId=" + id;
            imgDisplay.Visible = true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string idLang = drpLangId.SelectedValue;
            string name = txtName.Text.Trim();
            string shortDesc = txtShortDesc.Text;
            string idImg = hfdImgCode.Value;
            string fullDesc = ckey1.Text;
            string index = String.IsNullOrEmpty(txtIndex.Text) ? "0" : txtIndex.Text;
            bool isHotNews = chkHotNews.Checked;
            string status = drpStatus.Text;
            string idCategory=drpCategory.SelectedValue=="---"?"0":drpCategory.SelectedValue;
            string nameCategory = drpCategory.SelectedItem.Text == "---" ? "" : drpCategory.SelectedItem.Text;
            string review = String.IsNullOrEmpty(txtReview.Text)?"0":txtReview.Text.Trim();

            var repository = new NewsBL();
            var newsObj = new tbl_New();

            newsObj.LangId = int.Parse(idLang);
            newsObj.IndexShow = int.Parse(index);
            newsObj.NewsContent = fullDesc;
            newsObj.ShortDescription = shortDesc;
            newsObj.Status = drpStatus.Text;
            newsObj.Thumb = idImg;
            newsObj.Title = name;
            newsObj.CategoryNo = int.Parse(idCategory);
            newsObj.CategoryName = nameCategory;
            newsObj.ViewCount = int.Parse(review);

            repository.clsNewsInsert(newsObj);
            Response.Redirect("NewsMgmt.aspx");
        }
    }
}